import 'dart:developer';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_give/utils/LogUtils.dart';

import '../repository/datas/log_keys_data.dart';
import '../utils/constants.dart';
import '../utils/sp_utils.dart';

/// 请求头文件
/// 获取登录接口返回的cookie，并添加到请求头中去
class CookieInterceptor extends Interceptor {
  @override
  void onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    // 排除不需要token接口
    if (!options.path.contains('/login') &&
        !options.path.contains('/employee/add') &&
        !options.path.contains('/employee/register')) {
      // LogUtils.w("==========执行添加请求头==========");
      // token
      String token = await SpUtils.getString(Constants.SP_ZHSL_TOKEN);
      options.headers['x-access-token'] = token;
      // options.headers['Content-Type'] = 'application/json';
    }

    // 放行
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // if (response.requestOptions.path.contains("/login")) {
    // 取出登录接口返回的cookie
    // dynamic list = response.headers[HttpHeaders.setCookieHeader];
    // 遍历cookie，保存
    // List<String> cookieList = [];
    // if (list is List) {
    //   for (String? cookie in list) {
    //     cookieList.add(cookie ?? "");
    //     log("获取返回头 cookie：${cookie.toString()}");
    //   }
    // }
    // SpUtils.saveStringList(Constants.SP_COOKIE_LIST, cookieList);

    // LogKeysData? data = response.data;
    // LogUtils.w("请求头token数据=${data?.tangeLoginInfo}");
    // if (data?.tangeLoginInfo != null) {
    //
    // }
    // }

    // log("获取返回头：${response.headers.toString()}");
    super.onResponse(response, handler);
  }
}
